/**
 * @(#)SysMenuCtrl.java	07/05/2013
 *
 * Copyright (c) 2013 S9,Inc.All rights reserved.
 * Created by 2013-07-05 
 */
package cn.jdy.authority.admin;

import cn.jdy.authority.entity.SysMenu;
import cn.jdy.authority.entity.User;
import cn.jdy.authority.service.SysMenuService;
import cn.jdy.framework.constant.BaseConstant;
import cn.jdy.framework.exception.BaseException;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统菜单 liGan 2013-7-05
 */
@Controller
@RequestMapping("/sysMenu")
public class SysMenuCtrl {

	@Autowired
	protected HttpSession session;
	@Autowired
	private SysMenuService sysMenu;

	/**
	 * 显示用户拥有的菜单
	 *
	 * @return Map<String, Object>
	 */
	@RequestMapping("/getUserMenu")
	@ResponseBody
	public Map<String, Object> getUserMenu() {
		Map<String, Object> jsonMap = new HashMap<>();
		List<SysMenu> topMenu = new ArrayList<>();
		List<SysMenu> twoMenu = new ArrayList<>();
		try {
			// 获得系统所有菜单
			List<SysMenu> listSysMenu = sysMenu.findUserMenu(((User)session.getAttribute("user")).getId());
			if(listSysMenu != null && listSysMenu.size() != 0) {
				for(SysMenu menu : listSysMenu) {
					if(BaseConstant.TOP_SYSMENU_CODE.equals(menu.getPid())) {
						// 获得顶级菜单
						topMenu.add(menu);
					} else {
						// 获得二级菜单
						twoMenu.add(menu);
					}
				}
				List<SysMenu> twoList = null;
				boolean flag;
				for(SysMenu topM : topMenu) {
					twoList = new ArrayList<>();
					flag = false;
					for(SysMenu twoM : twoMenu) {
						if(topM.getId().equals(twoM.getPid())) {
							twoM.setPmenuName(topM.getMenuname());
							twoList.add(twoM);
							flag = true;
						}
					}
					if(flag) {
						topM.setChildren(twoList);
					}
				}
			}
			jsonMap.put("menus", topMenu);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "listSysMenu 显示系统所有菜单信息有误", e);
		}
		return jsonMap;
	}

	/**
	 * 根据父节点查询对应菜单 String pid 父节点 liGan 2013-08-07
	 *
	 * @return Map<String, Object>
	 */
	@RequestMapping("/getSysMenu")
	@ResponseBody
	public Map<String, Object> getSysMenu(String sid) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if("".equals(sid) || sid == null) {
				sid = BaseConstant.TOP_SYSMENU_CODE;
			}
			List<SysMenu> listSysMenu = sysMenu.find("pid", sid);
			jsonMap.put("sysMenu", listSysMenu);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "getSysMenu 根据父节点查询对应菜单有误", e);
		}
		return jsonMap;
	}

	/**
	 * 显示系统所有菜单信息 liGan 2013-08-07
	 *
	 * @return Map<String, Object>
	 */
	@RequestMapping("/listSysMenu")
	@ResponseBody
	public Map<String, Object> listSysMenu() {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		List<SysMenu> topMenu = new ArrayList<SysMenu>();
		List<SysMenu> twoMenu = new ArrayList<SysMenu>();
		try {
			// 获得系统所有菜单
			List<SysMenu> listSysMenu = sysMenu.listSysMenu(true);
			if(listSysMenu != null && listSysMenu.size() != 0) {
				for(SysMenu menu : listSysMenu) {
					if(BaseConstant.TOP_SYSMENU_CODE.equals(menu.getPid())) {
						// 获得顶级菜单
						topMenu.add(menu);
					} else {
						// 获得二级菜单
						twoMenu.add(menu);
					}
				}
				List<SysMenu> twoList = null;
				boolean flag;
				for(SysMenu topM : topMenu) {
					twoList = new ArrayList<SysMenu>();
					flag = false;
					for(SysMenu twoM : twoMenu) {
						if(topM.getId().equals(twoM.getPid())) {
							twoM.setPmenuName(topM.getMenuname());
							twoList.add(twoM);
							flag = true;
						}
					}
					if(flag) {
						topM.setChildren(twoList);
					}
				}
			}
			jsonMap.put("Rows", topMenu);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "listSysMenu 显示系统所有菜单信息有误", e);
		}
		return jsonMap;
	}

	/**
	 * 更改系统菜单
	 *
	 * @param menu 菜单信息 liGan 2013-08-07
	 * @return Map<String, Object>
	 */
	@RequestMapping("/updSysMenu")
	@ResponseBody
	public Map<String, Object> updSysMenu(SysMenu menu) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			sysMenu.update(menu);
			jsonMap.put("message", "success");
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "updSysMenu 更改系统菜单有误", e);
			jsonMap.put("message", "error");
		}
		return jsonMap;
	}

	/**
	 * 修改系统菜单状态 String menuId 菜单ID String isActive 菜单状态 liGan 2013-08-07
	 *
	 * @return Map<String, Object>
	 */
	@RequestMapping("/updSysMenuStatus")
	@ResponseBody
	public Map<String, Object> updSysMenuStatus(String menuId, String isActive) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			sysMenu.updSysMenuStatus(menuId, isActive);
			jsonMap.put("message", "success");
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "updSysMenuStatus 修改系统菜单状态有误", e);
			jsonMap.put("message", "error");
		}
		return jsonMap;
	}

	/**
	 * 查看系统菜单信息 String menuId 菜单ID liGan 2013-08-07
	 *
	 * @return ModelAndView
	 */
	@RequestMapping("/showSysMenu")
	@ResponseBody
	public SysMenu showSysMenu(String id) {
		return sysMenu.showSysMenu(id);
	}

	/**
	 * 保存系统菜单
	 *
	 * @param menu 菜单对象 liGan 2013-08-07
	 * @return ModelAndView
	 */
	@RequestMapping("/saveSysMenu")
	@ResponseBody
	public String saveSysMenu(SysMenu menu) {
		try {
			if(menu.getId() == null || "".equals(menu.getId())) {
				menu.setId(null);
				sysMenu.save(menu);
			} else {
				sysMenu.update(menu);
			}
			return "200";
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "saveSysMenu 保存系统菜单有误", e);
			return "500";
		}
	}

	/**
	 * 添加系统菜单 SysMenu menu 菜单对象 liGan 2013-08-07
	 *
	 * @return ModelAndView
	 */
	@RequestMapping("/addSysMenu")
	public ModelAndView addSysMenu(SysMenu menu) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/s9/menu/menu_add.jsp");
		return mv;
	}

	/**
	 * 角色菜单管理
	 *
	 * @param roleid 角色ID liGan 2013-08-07
	 * @return Map<String, Object>
	 */
	@RequestMapping("/findRoleMenu")
	@ResponseBody
	public Map<String, Object> findRoleMenu(String roleid) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			// 获得当前角色已存在的菜单权限
			List<String> list = sysMenu.findRoleMenu(roleid);
			jsonMap.put("roleMenu", list);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "findRoleMenu 角色菜单管理有误", e);
		}
		return jsonMap;
	}

	/**
	 * 添加角色权限
	 *
	 * @param roleid 角色ID
	 * @param menuid 菜单ID liGan 2013-08-07
	 * @return Map<String, Object>
	 */
	@RequestMapping("/addRoleMenu")
	@ResponseBody
	public Map<String, Object> addRoleMenu(String roleid, String menuid) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			sysMenu.addRoleMenu(roleid, menuid);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(this.getClass(), "addRoleMenu 添加角色权限有误", e);
		}
		return jsonMap;
	}

	/**
	 * 删除组织机构信息
	 *
	 * @return map
	 * @description: 删除组织机构信息, 返回删除是否成功信息
	 * @param: organIds {组织机构ID,组织机构ID ……}
	 * @author: wuzhiliang
	 * @date: 2013-08-05 16:53
	 */
	@RequestMapping("/delSysMenu")
	@ResponseBody
	public Map<String, Object> delSysMenu(String sysMenuIds) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(!StringUtil.isNullOrEmpty(sysMenuIds)) {
				sysMenu.delSysMenu(sysMenuIds);
				jsonMap.put("msg", "sucess");
			}
		} catch(BaseException e) {
			jsonMap.put("msg", "error");
			BaseLog.e(this.getClass(), "删除组织机构信息出错! delOrgan! ", e);
		}
		return jsonMap;
	}

	/**
	 * 批量更新
	 *
	 * @param type
	 * @return
	 */
	@RequestMapping("/updBatch")
	@ResponseBody
	public Map<String, Object> updBatch(String id, String type) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			updSysMenuStatus(id, type);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(getClass(), "商机信息更新异常！", e);
		}
		return jsonMap;
	}

}
